let STA = require('STA');

cc.Class({
    extends: cc.Component,

    properties: {
        SmallChapterCube:cc.Prefab,
        SmallChapterResultCube:cc.Prefab,
        smallChapterCubes:cc.Node,
        ChapterStartBtn:cc.Prefab,
        Lock:cc.Prefab,
        tapSound:cc.AudioClip
    },

    onLoad () {

        STA.log(STA.perfectChapters);


        this.cubeSize = 260;
        this.cubeGap = 24;
        this.unit = this.cubeSize + this.cubeGap;

        this.smallChaptersNumber = 15;
        this.lineNumber = 3;
        this.rowNumber = this.smallChaptersNumber / this.lineNumber;

        this.resultCubeSize = 20;
        this.resultCubeGap = 6;
        this.resultUnit = this.resultCubeSize + this.resultCubeGap;

        // 需要根据大关卡数去读取地图
        this.chapterStart = (STA.bigChapterNumber - 1) * 15 + 1;


        for(let i=0;i<this.smallChaptersNumber;i++){    // 关卡渲染
            let theX = i % this.lineNumber,
                theY = Math.floor(i / this.lineNumber);
            let cube = cc.instantiate(this.SmallChapterCube);
            this.smallChapterCubes.addChild(cube);
            cube.width = this.cubeSize;
            cube.height = this.cubeSize;
            cube.x = theX * this.unit;
            cube.y = -theY * this.unit;

            if(STA.bigChapterNumber == STA.bigChapterIndex + 1){
                if(i<STA.smallChapterIndex){ // 渲染已过关卡
                    this.renderResultCubes(cube,i);
                    this.addOnFactory(cube,i+1);
                }else if(i == STA.smallChapterIndex){ // 进行中的绿色按钮
                    let chapterStartBtn = cc.instantiate(this.ChapterStartBtn);
                    cube.addChild(chapterStartBtn);
                    this.addOnFactory(cube,i+1);
                }else{
                    let lock = cc.instantiate(this.Lock);
                    cube.addChild(lock);
                }
            }else{ 
                this.renderResultCubes(cube,i);
                this.addOnFactory(cube,i+1);
            }
        }
        this.smallChapterCubes.x += -1/2 * (this.lineNumber * this.unit - this.unit);
        this.smallChapterCubes.y += 1/2 * (this.rowNumber * this.unit - this.unit);
    },
    renderResultCubes(cube,i){
        STA.log(STA.perfectChapters[this.chapterStart + i])
        if(STA.perfectChapters[this.chapterStart + i]){  // 激活完美过关标记
            cube.getChildByName("perfectMark").active = true;
        } 

        let resultCubes = new cc.Node();  // 答案图块的容器
        let maxX = 0, maxY = 0;  // 记录结果的宽高，用于调整位置

        let r = 100 + parseInt((Math.random() * 100)),
            g = 100 + parseInt((Math.random() * 100)),
            b = 100 + parseInt((Math.random() * 100));

        // 读取答案并生成对应图案
        cc.loader.loadRes("results/"+(this.chapterStart + i), (err,data)=>{
            if(err) cc.error(err);
            let result = data.json.link;
            for(let i=0,l=result.length;i<l-1;i++){

                let smallChapterResultCube = cc.instantiate(this.SmallChapterResultCube);
                smallChapterResultCube.width = this.resultCubeSize;
                smallChapterResultCube.height = this.resultCubeSize;
                smallChapterResultCube.x = result[i].x * this.resultUnit;
                smallChapterResultCube.y = -result[i].y * this.resultUnit;

                let dir = this.getDir(result[i], result[i+1]);
                if(dir == 0){
                    smallChapterResultCube.height += this.resultUnit;
                    smallChapterResultCube.y += 1/2 * this.resultUnit;
                }else if(dir == 1){
                    smallChapterResultCube.width += this.resultUnit;
                    smallChapterResultCube.x += 1/2 * this.resultUnit;
                }else if(dir == 2){
                    smallChapterResultCube.height += this.resultUnit;
                    smallChapterResultCube.y -= 1/2 * this.resultUnit;
                }else if(dir == 3){
                    smallChapterResultCube.width += this.resultUnit;
                    smallChapterResultCube.x -= 1/2 * this.resultUnit;
                }

                smallChapterResultCube.color = new cc.Color(r,g,b);

                if(result[i].x > maxX){
                    maxX = result[i].x
                }
                if(result[i].y > maxY){
                    maxY = result[i].y;
                }

                resultCubes.x = -1/2 * (maxX * this.resultUnit) - this.resultCubeGap; // 调整位置
                resultCubes.y = 1/2 * (maxY * this.resultUnit);
                resultCubes.addChild(smallChapterResultCube);
            }
            cube.addChild(resultCubes);
        });
    },

    addOnFactory(cube,smallChapterNumber){
        cube.on(cc.Node.EventType.TOUCH_START, (event)=>{
            cube.runAction(cc.sequence(
                cc.scaleTo(0.1,1.1),
                cc.scaleTo(0.1,1),
                cc.callFunc(()=>{
                    STA.smallChapterNumber = smallChapterNumber;
                    cc.director.preloadScene("gamePlay", ()=>{
                        cc.director.loadScene("gamePlay");
                    });
                })
            ));
            STA.playAudio(this.tapSound);
        });
    },

    getDir(iniNode,nowNode){
        if(iniNode.x == nowNode.x){
            if(iniNode.y > nowNode.y){ // up
                return 0;
            }else{ // down
                return 2;
            }
        }else if(iniNode.y == nowNode.y){
            if(iniNode.x > nowNode.x){ // left
                return 3;
            }else{ // right
                return 1;
            }
        }
    },

    start () {

    },

    back(){
        STA.playAudio(this.tapSound);
        cc.director.loadScene("bigChapters");
    },

    update (dt) {},
});
